package com.ltv.xiangqibook.web;

import java.sql.SQLException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.ltv.xiangqibook.common.XiangQiBookConfiguration;

public class XiangQiBookListener implements ServletContextListener {
    private static final Logger logger = LogManager.getLogger(XiangQiBookListener.class);
    
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        try {
            ConnectionPool.getInstance().closeAllConnections();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
            XiangQiBookConfiguration.getInstance().loadConfiguration(
                    event.getServletContext().getRealPath(XiangQiBookConfiguration.CONFIG_FILE_NAME));
            
            ConnectionPool.getInstance();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

}
